package com.informatics.polymer.server.opsin;

import java.io.File;
import java.io.IOException;

import nu.xom.Element;

import org.apache.commons.io.FileUtils;
import org.restlet.Context;
import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.data.Status;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.ResourceException;
import org.restlet.resource.StringRepresentation;
import org.restlet.resource.Variant;

import uk.ac.cam.ch.wwmm.opsin.NameToStructure;
/**
 * OPSIN restlet. Essentially converts an IUPAC named chemical entity into a chemical
 * markup language structure.
 * @author ed
 * @version 1.0
 */
public class NameToStructureConversionResource extends Resource {
	/**
	 * String representation of IUPAC name.
	 */
	private String iupacName;
	/**
	 * 
	 * @return String representation of IUPAC name.
	 */
	private String getIupacName(){
		return iupacName;
	}
	/**
	 * 
	 * @param name - String representation of IUPAC name.
	 */
	private void setIupacName(String name){
		iupacName = name;
	}
	/**
	 * Initiate the restlet.
	 */
	@Override
	public void init(Context context, Request request, Response response) {
		super.init(context, request, response);
		getVariants().add(new Variant(MediaType.TEXT_PLAIN));
		this.setModifiable(true);
	}
	/**
	 * Method allows forms to be posted to restlet.
	 */
	@Override
	public boolean allowPost() {
		return true;
	}
	/**
	 * Constructs a NameToStructureConversionResource.
	 * @param context
	 * @param request
	 * @param response
	 */
	public NameToStructureConversionResource(Context context, Request request, Response response){
		super(context,request,response);
		getVariants().add(new Variant(MediaType.TEXT_PLAIN));
	}
	/**
	 * Method deals with the form posted to the restlet.
	 */
	@Override
	public void post(Representation entity) {
		Form form = new Form(entity); 
		setIupacName(form.getValues("IUPACName"));
		Element cmlElement = null;
		try {
			NameToStructure nameToStructure = NameToStructure.getInstance();
			cmlElement = nameToStructure.parseToCML(getIupacName());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		String finalResult = cmlElement.toXML();
		StringRepresentation str = new StringRepresentation(finalResult);
		getResponse().setEntity(str);
		String f = "/OPSINOutput"+".cml";
		String curDir = new String(System.getProperty("user.dir"));
		File opsinXml = new File(curDir+f);
		//Use apache io
		try {
			FileUtils.writeStringToFile(opsinXml, finalResult);
		} catch (IOException e1) {
			e1.printStackTrace();
		}

	}
	// Respond to GET
	@Override
	public Representation represent (Variant variant) throws ResourceException{
		Representation resource = null;
			if (!MediaType.TEXT_PLAIN.includes(variant.getMediaType())) {
				throw new ResourceException(Status.CLIENT_ERROR_NOT_ACCEPTABLE);
			}
			else{
				StringBuffer sb = new StringBuffer();
				try {
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}		
				resource = new StringRepresentation(sb.toString(), MediaType.TEXT_PLAIN); 
				return resource;
			}

	}

}
